package com.aceitunaproject.webclient.client;


import java.util.HashMap;
import java.util.Map;

import com.aceitunaproject.shared.gamelement.Installation;
import com.aceitunaproject.shared.gamelement.Settlement;
import com.aceitunaproject.shared.gamelement.Unit;
import com.aceitunaproject.shared.map.MapTile;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Panel to show details about a tile
 * 
 * @author Federico Bergstein
 * 
 */
public class DetailsPanel extends ScrollPanel {

	private static final long serialVersionUID = 1L;
	private final VerticalPanel thisPanel = new VerticalPanel();
	private Board view;

	Map<String, Unit> tileUnits;
	Map<String, Installation> tileInstallations;
	Settlement tileSettlement;

	public DetailsPanel(Board view) {
		this.view = view;
		this.add(thisPanel);
	}



	private Button createButton(Unit element) {
		tileUnits.put(element.getId().toString(), element);

		return createButton("unit", element.getName(), "U:"
				+ element.getId().toString());
	}

	private Button createButton(Installation element) {
		tileInstallations.put(element.getId().toString(), element);

		return createButton("Installation", element.getName(), "I:"
				+ element.getId().toString());
	}

	private Button createButton(Settlement element) {

		tileSettlement = element;
		return createButton("Settlement", element.getName(), "S:"
				+ element.getId().toString());
	}

	private Button createButton(String imageName, String text, final String id) {

		ImageElement image = new BoardImageProvider().getThingImage(new WebThing(imageName));


		image.setWidth(15);
		image.setHeight(15);
		Button button = new Button(text);// , icon);
		button.getElement().appendChild(image);
		button.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				actionPerformed(id);
				
			}
		});
	
		return button;
	}

	public void setTile(MapTile mapTile) {

		thisPanel.clear();

		tileUnits = new HashMap<String, Unit>();
		tileInstallations = new HashMap<String, Installation>();
		tileSettlement = null;

		 
		Label label = new Label(mapTile.getTerrain().get(0).getName());
		label.getElement().insertFirst(new BoardImageProvider().getCenter(new WebTile(mapTile)));
		thisPanel.add(label);

		for (Unit unit : mapTile.getUnits()) {

			thisPanel.add(createButton(unit));

		}
		for (Installation installation : mapTile.getInstallations()) {

			thisPanel.add(createButton(installation));

		}

		Settlement theSettlement = mapTile.getSettlement();
		if (theSettlement != null) {
			thisPanel.add(createButton(theSettlement));
		}


	}


	public void actionPerformed(String action) {

		String[] a = action.split(":");

		if(a[0].equals("U"))
			view.unitSelected(tileUnits.get(a[1]));

		if(a[0].equals("I"))
			view.installationSelected(tileInstallations.get(a[1]));

		if(a[0].equals("S"))
			view.settlementSelected(tileSettlement);

	

	}

}
